import ProForm, {
  ProFormDateRangePicker,
  ProFormDependency,
  ProFormDigit,
  ProFormRadio,
  ProFormSelect,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-form';
import {PageContainer} from '@ant-design/pro-layout';
import {Card, message, Row, Col, Space, Typography, Upload, Button, Modal} from 'antd';
import {
  DisciEvalSituationApi,
  DownloadApi,
  FillApi, GetSaveApi, SaveApi,
  SchoolCheckShow,
  SchoolPass, SchoolReject, ShowApi,
  UploadApi
} from "@/services/School/SchoolApi";
import {history, request} from 'umi';
import {ServerMessages} from "@/services/ServerMessages";
import type {UploadProps} from 'antd';
import {DownloadOutlined, UploadOutlined} from "@ant-design/icons";
import {getUserInfo, ShowError} from "@/utils/format";
import React, {useState} from "react";
import {useRef} from 'react';
import Submitter from "@/pages/Syl/Common/Submitter";

const {Text} = Typography;

export default () => {
  const status_is_check = history.location.query.id && history.location.query.fill_id
  const status_is_show = history.location.query.flag === 'display'
  const formRef = useRef();
  //上传
  const upload = UploadApi('/api/index/disci-cons/progress/disci-eval-situation-template',
    (info) => {
      console.log("@success", info)
      if (info.file.response.status === 0) {
        // message.success(info.file.response.message)
        // history.push('/disci-cons/progress')
        const data = info.file.response.data.data_1_1_2
        console.log(data)
        formRef?.current?.setFieldsValue({
          "discipline_eval_turn_5": data[0].discipline_eval_result,
          "discipline_eval_turn_6": data[1].discipline_eval_result,
        })
      } else {
        ShowError(info.file.response.message)
      }
    })
  //下载
  const download = "1_1_2"
  const handleSaveBtn = () => {
    console.log("@Save", download)
    const values = formRef.current.getFieldFormatValue()
    const data = {
      fill_id: download,
      data: [
        {
          'discipline_eval_turn': 5,
          'discipline_eval_result': values.discipline_eval_turn_5
        },
        {
          'discipline_eval_turn': 6,
          'discipline_eval_result': values.discipline_eval_turn_6
        },
      ]
    }
    console.log("@请求数据", data)
    SaveApi('/api/temp-save', data).then(response => {
      if (response.status === 0) {
        Modal.success({
          title: <span>保存成功！</span>,
          onOk() {
            window.location.reload()
          }
        })
      } else {
        ShowError(response.message)
      }
    }, () => {
      ShowError(ServerMessages.error_message)
    })
  }
  return (
    <PageContainer
      title="学科评估情况"
      content={<Text type="danger">1. 监测点内涵：第五轮评估情况。预计第六轮学科评估情况.
        <br/>2. 学科评估结果：请用A+, A, B+, B, B-, C+, C, C-, 无填写。
    </Text>}>
      <Card bordered={false}>
        <ProForm
          request={() => {
            return new Promise(resolve => {
              if (status_is_check) {
                SchoolCheckShow(history.location.query.id, history.location.query.fill_id)
                  .then(response => {
                    console.log("@response", response)
                    if (response.status === 0) {
                      let initVal = {
                        "discipline_eval_turn_5": response.message[0].discipline_eval_result,
                        "discipline_eval_turn_6": response.message[1].discipline_eval_result,
                      }
                      resolve(initVal)
                    } else {
                      resolve(true)
                    }
                  })
              } else if (status_is_show) {
                ShowApi(download).then(response => {
                  console.log("@response", response)
                  if (response.status === 0) {
                    let initVal = {
                      "discipline_eval_turn_5": response.data[0].discipline_eval_result,
                      "discipline_eval_turn_6": response.data[1].discipline_eval_result,
                    }
                    resolve(initVal)
                  }
                })
              } else {
                GetSaveApi('/api/show-temp-save', download).then(response => {
                  console.log("@response", response)
                  if (response.status === 0) {
                    let initVal = {
                      "discipline_eval_turn_5": response.data[0].discipline_eval_result,
                      "discipline_eval_turn_6": response.data[1].discipline_eval_result,
                    }
                    resolve(initVal)
                  } else {
                    resolve(true)
                  }
                })
              }
            })
          }}
          onFinish={async (values) => {
            const queryString = {
              'data_1_1_2': [
                {
                  'discipline_eval_turn': 5,
                  'discipline_eval_result': values.discipline_eval_turn_5
                },
                {
                  'discipline_eval_turn': 6,
                  'discipline_eval_result': values.discipline_eval_turn_6
                },
              ],
            }
            console.log('@提交数据', queryString)
            FillApi('/api/index/disci-cons/progress/disci-eval-situation', queryString, (response) => {
              message.success(response.message)
              history.push('/disci-cons/progress')
            }, (response) => {
              ShowError(response.message)
            }, () => {
              ShowError(ServerMessages.error_message)
            })
          }}
          formRef={formRef}
          hideRequiredMark
          style={{margin: 'auto', marginTop: 8}}
          name="basic"
          layout="horizontal"
          labelCol={{span: 8}}
          submitter={{
            render: (props, doms) => {
              return <Submitter
                upload={upload}
                download={download}
                doms={doms}
                save={handleSaveBtn}
              />
            },
            resetButtonProps: {
              style: {
                // 隐藏重置按钮
                display: 'none',
              },
            },
            submitButtonProps: {
              style: {
                marginLeft: '-8px'
              }
            },
          }}
        >
          {/*<ProFormSelect*/}
          {/*  name="discipline_eval_turn_4"*/}
          {/*  label="第四轮评估结果"*/}
          {/*  valueEnum={{*/}
          {/*    "A+": 'A+',*/}
          {/*    "A": 'A',*/}
          {/*    "A-": 'A-',*/}
          {/*    "B+": 'B+',*/}
          {/*    "B": 'B',*/}
          {/*    "B-": 'B-',*/}
          {/*    "C+": 'C+',*/}
          {/*    "C": 'C',*/}
          {/*    "C-": 'C-',*/}
          {/*    "无": '无',*/}
          {/*  }}*/}
          {/*  placeholder="请选择"*/}
          {/*  allowClear={false}*/}
          {/*  rules={[{required: true, message: '请选择'}]}*/}
          {/*  style={{width: 328}}*/}
          {/*  disabled={status_is_check || status_is_show}*/}
          {/*/>*/}
          <ProFormSelect
            name="discipline_eval_turn_5"
            label="第五轮评估结果"
            valueEnum={{
              "A+": 'A+',
              "A": 'A',
              "A-": 'A-',
              "B+": 'B+',
              "B": 'B',
              "B-": 'B-',
              "C+": 'C+',
              "C": 'C',
              "C-": 'C-',
              "无": '无',
            }}
            placeholder="请选择"
            allowClear={false}
            rules={[{required: true, message: '请选择'}]}
            style={{width: 328}}
            disabled={status_is_check || status_is_show}
          />
          <ProFormSelect
            name="discipline_eval_turn_6"
            label="第六轮评估结果"
            valueEnum={{
              "A+": 'A+',
              "A": 'A',
              "A-": 'A-',
              "B+": 'B+',
              "B": 'B',
              "B-": 'B-',
              "C+": 'C+',
              "C": 'C',
              "C-": 'C-',
              "无": '无',
            }}
            placeholder="请选择"
            allowClear={false}
            rules={[{required: true, message: '请选择'}]}
            style={{width: 328}}
            disabled={status_is_check || status_is_show}
          />
        </ProForm>
      </Card>
    </PageContainer>

  );
}
//
// class DisciEvalSituation extends Component {
//   render() {
//
//
//     return
//   }
// }
//
// export default DisciEvalSituation;

//
// import { UploadOutlined } from '@ant-design/icons';
// import {Button, message, Upload, UploadProps} from 'antd';
// import React from 'react';
// import {getUserInfo} from "@/utils/format";
// import ServiceEnvs from "@/services/ServiceEnvs";
// const userInfo = getUserInfo()
// const baseUrl = ServiceEnvs.baseUrl
// const props: UploadProps = {
//   name: 'file',
//   action: baseUrl+'/api/index/disci-cons/progress/disci-eval-situation-template',
//   headers: {
//     'Authorization': userInfo.token
//   },
//   onChange(info) {
//     if (info.file.status !== 'uploading') {
//       console.log(info.file, info.fileList);
//     }
//     if (info.file.status === 'done') {
//       message.success(`${info.file.name} file uploaded successfully`);
//     } else if (info.file.status === 'error') {
//       ShowError(`${info.file.name} file upload failed.`);
//     }
//   },
// };
//
// const App = () => (
//   <Upload {...props}>
//     <Button icon={<UploadOutlined />}>Click to Upload</Button>
//   </Upload>
// );
//
// export default App;
